<!-- 商品分类组件说明 -->
<template>
  <div class='cate'>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right"> 
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb> 
     <!-- 卡片视图 -->
    <el-card class="box-card">
      <el-row>
        <el-col>
          <!-- 添加分类按钮 -->
          <el-button type="primary" @click="addCateDialog">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 分类列表表格 -->
      <tree-table :data="cateList" :columns="columns" :selection-type="false" :expand-type="false"
      :show-index="true" index-text="#" border :show-row-hover="false">
        <!-- 是否有效列 -->
        <template slot="isok" slot-scope="scope">
          <i v-if="scope.row.cat_deleted === false" style="color:lightgreen" class="el-icon-success"></i>
          <i v-else style="color: red" class="el-icon-error"></i>
        </template>
        <!-- 排序 -->
        <template slot="sort" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag size="mini" v-else-if="scope.row.cat_level === 1" type="success">二级</el-tag>
          <el-tag size="mini" v-else type="warning">三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope="scope">
          <el-button size="mini" @click="editCateBtn(scope.row)" type="primary">编辑</el-button>
          <el-button size="mini" @click="deleteCateBtn(scope.row)" type="danger">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页功能 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="queryInfo.pagenum"
      :page-size="queryInfo.pagesize" layout="total, prev, pager, next, jumper" :total="total">
      </el-pagination>
      <!-- 添加分类对话框 -->
      <el-dialog title="添加分类" :visible.sync="addCateDialogVisible" width="50%" @close="addCateDialogClose">
        <!-- 内容区域 -->
        <el-form :model="addCateRuleList" :rules="addCateRules" ref="addCateRuleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addCateRuleList.cat_name"></el-input>
          </el-form-item>
          <!-- 级联选择器 -->
          <el-form-item label="父级分类">
            <el-cascader v-model="parentCateId" :options="parentCateList" :props="cascaderProps" @change="parentCateChanged" clearable>
            </el-cascader>
          </el-form-item>
        </el-form>
        <!-- 底部区域 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="addCateDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCate">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 编辑分类对话框 -->
      <el-dialog title="编辑分类" :visible.sync="editCateDialogVisible" width="50%" @close="editCateDialogClose">
        <!-- 内容区域 -->
        <el-form :model="editCateName" :rules="addCateRules" ref="editCateRuleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="editCateName.cat_name"></el-input>
          </el-form-item>
        </el-form>
         <!-- 底部区域 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="editCateDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editCate">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
  //import x from ''
  export default {
    components: {

    },
    data () {
      return {
        //存储商品分类列表数据
        cateList:[],
        //父级商品分类
        parentCateList:[],
        //表格列信息
        columns: [
          {
            label: '分类名称',
            prop: 'cat_name',
          },
          {
            label: '是否有效',
            prop: 'cat_deleted',
            //  自定义模版列
            type: 'template',
            template: 'isok'
          },
          {
            label: '排序',
            prop: 'cat_level',
            type: 'template',
            template: 'sort'
          },
          {
            label: '操作',
            type: 'template',
            template: 'opt'
          },
          {

          }
        ],
        //分页显示数据
        queryInfo:{
          pagenum: 1,
          pagesize: 10
        },
        //分类数据总条数
        total: 0,
        //添加分类对话框显示/隐藏
        addCateDialogVisible: false,
        // 添加分类信息绑定
        addCateRuleList: {
          cat_name: '',
          // 默认为1级分类
          cat_pid: 0,
          cat_level: 0
        },
        //添加分类表单规则
        addCateRules: {
          cat_name:[
            { required: true, message: '请输入分类名称', trigger: 'blur' },
          ]
        },
        //添加分类中选取的类型id
        parentCateId:[],
        // 级联分类器属性
        cascaderProps:{
          label: 'cat_name',
          value: 'cat_id',
          expandTrigger: 'hover',
          children: 'children',
          checkStrictly: true
        },
        // 添加分类对话框显示/隐藏
        editCateDialogVisible: false,
        //编辑分类信息对话框 信息存储
        editCateName: {
          cat_name:'',
          cat_id:''
        }
      };
    },
    created() {
      this.getCateList()
    },
    methods: {
      // 获取商品分类列表
      async getCateList(){
        const{data:res} = await this.$http.get('categories',{params:this.queryInfo})
        if(res.meta.status !== 200){
          return this.$message.error(res.meta.msg)
        }else{
          this.cateList = res.data.result
          this.total = res.data.total
          console.log(this.cateList);
        }
      },
      // 分页方法： 改变每页条数
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.queryInfo.pagesize = val
        this.getCateList()
      },
      // 分页方法： 跳转到当前页
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.queryInfo.pagenum = val
        this.getCateList()
      },
      //点击添加分类按钮
      addCateDialog(){
        //显示选择分类相关信息
        this.getParentCateList()
        this.addCateDialogVisible = true
      },
      // 监听选择父类类型改变事件
      parentCateChanged() {
        console.log(this.parentCateId);
        if(this.parentCateId.length !== 0){
          this.addCateRuleList.cat_pid = this.parentCateId.length
          this.addCateRuleList.cat_id = this.parentCateId[this.parentCateId.length-1]
        }else{
          this.addCateRuleList.cat_pid = 0
          this.addCateRuleList.cat_id = 0
        }
        
      },
      //获取父级分类列表
      async getParentCateList(){
        const{data:res} = await this.$http.get('categories',{params: {type: 2}})
        if(res.meta.status !== 200){
          return this.$message.error(res.meta.msg)
        }else{
          this.parentCateList = res.data
          console.log(this.parentCateList);
        }
      },
      //添加商品分类
      addCate(){
        this.$refs.addCateRuleForm.validate(async (valid) => {
          if (!valid) {
            return this.$message.error("添加分类出错")
          } else {
            const{data:res} = await this.$http.post('categories',this.addCateRuleList)
            if(res.meta.status !== 201) return this.$message.error(res.meta.msg)
            this.getCateList()
            this.$message.success('添加分类成功')
            this.addCateDialogVisible = false
          }
        })
      },
      //添加角色对话框关闭时 清除内容
      addCateDialogClose(){
        this.$refs.addCateRuleForm.resetFields()
        this.addCateRuleList.cat_pid = 0
        this.parentCateId = []
      },
      // 编辑分类对话框关闭时 清除内容
      editCateDialogClose(){
        this.$refs.editCateRuleForm.resetFields()
      },
      //点击编辑按钮 显示编辑分类对话框
      editCateBtn(cate){
        this.editCateName.cat_name = cate.cat_name 
        this.editCateName.cat_id = cate.cat_id
        this.editCateDialogVisible = true
      },
      //确定编辑分类按钮
      editCate(){
        this.$refs.editCateRuleForm.validate(async (valid) =>{
          if(!valid){
            this.$message.error('修改分类名称失败')
          }else{
            const{data:res} = await this.$http.put(`categories/${this.editCateName.cat_id}`,{cat_name:this.editCateName.cat_name})
            if(res.meta.status !== 200) {
              return this.$message.error(res.meta.msg)
            }else{
              this.getCateList()
              this.$message.success('编辑分类成功')
            }
            this.editCateDialogVisible = false
          }
        })
      },
      //点击删除按钮
      async deleteCateBtn(cate){
        const str = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        if(str !== 'confirm'){
          return this.$message.info('已取消删除')
        }else{
          const{data:res} = await this.$http.delete(`categories/${cate.cat_id}`)
          if(res.meta.status !== 200) {
            return this.$message.error(res.meta.msg)
          }else{
            this.$message.success('删除成功')
            this.getCateList()
          }
        }
      }

    },
  }
</script>

<style lang='less' scoped>
//@import url()
.zk-table{
  margin-top: 15px;
}
.el-cascader{
  width: 100%;
}
</style>